*Input dataset with estimates and organize for analysis

use markups_for_analysis, clear

set matsize 10000

keep CIIU3_3D id time cpc3 num_prod dest markup logprod r l k i q_prod_ijtu q_sale_ijt p_fjt  ///
quant l_markup mult_prod dexp_ftp mc_ftp b a al ak ai ///
d_rank1 adrat1 adrat2 ad_pq_it l_tot k_tot i_tot ego  int_input_price

*Adjust productivity for re-scaling of inputs in estimation
gen double logprod_true = logprod+ ln(1000)-(al+ak+ai)*ln(1000)-al*3-ai*5
drop logprod
ren logprod_true logprod

compress



***TABLES***


*Table 1--summary stats

preserve
*Generate additional revenue variables
gen r_ftpd = p_fjt * quant
gen r_ftpd_exp = r_ftpd if dest ~= "dom"
gen r_ftpd_dom = r_ftpd if dest == "dom"

bys id time: egen r_ft_exp = sum(r_ftpd_exp)
bys id time: egen r_ft_dom = sum(r_ftpd_dom)

bys id time cpc3: egen r_ftp_exp = sum(r_ftpd_exp)
bys id time cpc3: egen r_ftp_dom = sum(r_ftpd_dom)


*Keep only one observation per firm-product-time triplet
keep if dest =="dom" 

*Number of Firms
unique id, by(CIIU) generate(n_firms)

*Number of Firm-Product-Time Observations
unique id time cpc3, by(CIIU) gen(n_obs_ftp)

*Number of Firm-Time Observations
unique id time, by(CIIU) gen(n_obs_ft)

*Number of Products
unique cpc3, by(CIIU) generate(n_products)

*Number of Products Exported
unique cpc3 if r_ftp_exp ~= 0, by(CIIU) generate(n_products_exp)

*Percentage of Exporting Firms
unique id if r_ftp_exp ~= 0, by(CIIU) generate(n_firms_exp)
bys CIIU: egen num1 = max(n_firms_exp)
bys CIIU: egen num1_test = sd(n_firms_exp)

bys CIIU: egen den1 = max(n_firms)
bys CIIU: egen den1_test = sd(n_firms)

gen percent_exp_firms = num1 / den1
tabstat percent_exp_firms, by(CIIU) stats(mean sd)
drop num1 num1_test den1 den1_test

*Percentage of Single-Product Firms
unique id time if num_prod ==1, by(CIIU) generate(n_firms_single_prod)

bys CIIU: egen num2 = max(n_firms_single_prod)
bys CIIU: egen den2 = max(n_obs_ft)

gen percent_sp_firms = num2 / den2
tabstat percent_sp_firms, by(CIIU) stats(mean sd)
drop num2 den2 

*Export Results
collapse  n_obs* n_firms n_products n_products_exp percent_exp_firms percent_sp_firms, by(CIIU)
order CIIU percent_exp_firms n_firms n_products n_products_exp percent_sp_firms n_obs_ftp n_obs_ft
outsheet CIIU percent_exp_firms n_firms n_products n_products_exp percent_sp_firms n_obs_ftp n_obs_ft using table1.csv, comma replace

restore



*****************************************************************
*KEEP ONLY MAIN PRODUCT--all observations used for Table 1 only
*****************************************************************
keep if d_rank1 == 1
****************************************************


*Table 2--PF estimates
gen double rts = al+ak+ai
preserve
collapse al ak ai rts, by(CIIU3_3D)
order CIIU al ak ai rts
li
outsheet using table2.csv, comma replace
restore


*Table 3--Markups
preserve
	*Quantity
	bys id cpc3: egen q_fp = sum(quant)
	*Markups
	gen markup_temp = markup*quant
	bys id cpc3: egen markup_fp = sum(markup_temp)
	drop markup_temp
	replace markup_fp = markup_fp / q_fp
collapse markup_fp, by(CIIU id)
bys CIIU: egen med_markup = median(markup_fp)
bys CIIU: egen mean_markup = mean(markup_fp)
collapse med_markup mean_markup, by(CIIU)
li CIIU med_markup mean_markup
set obs `=_N+1'
foreach var of varlist med - mean  {
	egen m_`var' = mean(`var')
	replace `var' = m_`var' if `var' == .
	drop m_`var'
}
li
outsheet using table3.csv, comma replace
restore


*Table 4--markups, productivity, and marginal cost
egen cpc_time=group(cpc3 time)  /* product-year FE */

gen l_mc_ftp=ln(mc_ftp)

capture drop temp
gen temp = markup*quant
bys id cpc3 time: egen markup_ftp_num = sum(temp)
bys id cpc3 time: egen markup_ftp_den = sum(quant)
gen markup_ftp = markup_ftp_num / markup_ftp_den

gen l_markup_ftp = ln(markup_ftp)
drop temp markup_ftp_num markup_ftp_den


reg logprod i.cpc_time dexp_ftp   if dest=="dom"
estimates store prod

reg l_markup i.cpc_time dexp_ftp   if dest=="dom"
estimates store mup

reg l_mc_ftp i.cpc_time dexp_ftp   if dest=="dom"
estimates store mc

reg l_markup_ftp dexp_ftp i.cpc_time if dest =="dom"
estimates store mu_agg


estout prod mc mup mu_agg, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) indicate("Product-Time FE=*.cpc_time")
esttab prod mc mup mu_agg using table4.csv, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) indicate("Product-Time FE=*.cpc_time") replace


*Table 5--within firms and markets
gen dummy_foreign_market=(dest~="dom")
egen cpc_firm_time=group(cpc3 id time)

areg l_markup dummy_foreign_market, absorb(cpc_firm_time)
estimates store mupdom_for

*Merge in Country data
sort dest time
merge dest time using country_gravity_chars

ren gdpconstant2010us gdp_constant_2000_usd 
ren gdppercapitaconstant2010us gdp_const_2000_usd_per_cap 

gen l_gdp_constant_2000_usd=ln(gdp_constant_2000_usd)
gen l_gdp_const_2000_usd_per_cap=ln(gdp_const_2000_usd_per_cap)
gen l_dist=ln(dist)

areg l_markup l_gdp_constant_2000_usd l_gdp_const_2000_usd_per_cap l_dist com_lang  if dest~="dom", absorb(cpc_firm_time)
estimates store mupgravity
estout  mupdom_for mupgravity , cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) 
esttab  mupdom_for mupgravity using table5.csv, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) replace


*Table 6--markups and quantity sold
gen l_quant=ln(quant)
gen quant_tot = q_sale_ijt
gen l_quant_tot=ln(quant_tot)
egen cpc_market_time=group(cpc3 dest time)
egen cpc_firm_market=group(cpc3 id dest)

areg l_markup l_quant, absorb(cpc_firm_market)
estimates store quant_1
areg l_markup l_quant, absorb(cpc_market_time)
estimates store quant_2
areg l_markup l_quant, absorb(cpc_firm_time)
estimates store quant_3

gen dexp_ftp_l_quant_inter = l_quant*dexp_ftp 
areg l_markup l_quant dexp_ftp dexp_ftp_l_quant_inter if dest =="dom", absorb(cpc_time)
estimates store quant_4

estout quant_*, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0))
esttab quant_* using table6.csv, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) replace


*Table 7--demand, costs, and profit measures

*Flag one observation per firm-product-time grouping
gen one = 1
bys id time cpc3: gen count = sum(one)
drop one

*Scale Values for Non-Exporters
tab cpc_time, gen(d_pt_)
qreg l_quant_tot d_pt_* if dexp_ftp == 0 & count == 1
predict l_m_quant, xb
qreg l d_pt_* if dexp_ftp == 0 & count == 1
predict m_l, xb
qreg k d_pt_* if dexp_ftp == 0 & count == 1
predict m_k, xb
gen m_quant = exp(l_m_quant)

*Scale Values for Exporters
qreg l_quant_tot d_pt_* if dexp_ftp == 1 & count == 1
predict l_m_quant_alt, xb
qreg l d_pt_* if dexp_ftp == 1 & count == 1
predict m_l_alt, xb
qreg k d_pt_* if dexp_ftp == 1 & count == 1
predict m_k_alt, xb
gen m_quant_alt = exp(l_m_quant_alt)

*Set Scale
gen l_ms=ak*m_k + al*m_l
gen ms = exp(l_ms)

gen l_ms_alt=ak*m_k_alt + al*m_l_alt
gen ms_alt = exp(l_ms_alt)

gen l_s=ak*k + al*l 
gen s = exp(l_s)

*Profit Index and Components
gen l_a = ln(a)
gen l_b = ln(-b)
gen l_c = l_mc_ftp - ln(q_prod_ijtu)
gen l_c0 = l_c - ( (1-2*ai)/ai ) * ln(q_prod_ijtu) + (1/ai)*l_s 
gen c0 = exp(l_c0)
gen c = exp(l_c)

gen b_c=-b*2+c0*(m_quant^((1-2*ai)/ai))*((1/ms)^(1/ai))
gen b_c_alt=-b*2+c0*(m_quant_alt^((1-2*ai)/ai))*((1/ms_alt)^(1/ai))
gen l_b_c=ln(b_c) 
gen l_b_c_alt=ln(b_c_alt) 

gen l_profit=ln(0.5)+2*l_a-l_b_c  
gen l_profit_alt=ln(0.5)+2*l_a-l_b_c_alt 

*Regressions
reg l_a i.cpc_time dexp_ftp   if dest=="dom"
estimates store a

reg l_b i.cpc_time dexp_ftp   if dest=="dom"
estimates store b

reg l_c0 i.cpc_time dexp_ftp   if dest=="dom"
estimates store c0

reg logprod i.cpc_time dexp_ftp   if dest=="dom"
estimates store prod

reg l_profit i.cpc_time dexp_ftp   if dest=="dom"
estimates store prof

reg l_profit_alt i.cpc_time dexp_ftp   if dest=="dom"
estimates store prof_alt

*Export Status
estout a b c0 prod prof prof_alt, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) indicate("Product-Year FE=*.cpc_time")
esttab a b c0 prod prof prof_alt using table7.csv, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) indicate("Product-Year FE=*.cpc_time") replace
 
 
*Table 8--Cost and Demand Heterogeneity and Exporting
gen a2b = (a^2)/(-b)
gen ln_a2b = ln(a2b)
gen ln_c0b = l_c0 - ln(-b)

reg ln_c0b dexp_ftp i.cpc_time if dest == "dom"
estimates store het1
reg ln_a2b dexp_ftp i.cpc_time if dest == "dom"
estimates store het2
reg ln_c0b ln_a2b dexp_ftp i.cpc_time if dest == "dom"
estimates store het3
reg ln_a2b ln_c0b dexp_ftp i.cpc_time if dest == "dom"
estimates store het4

estout het1 het2 het3 het4, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) indicate("Product FE=*.cpc_time")
esttab het1 het2 het3 het4 using table8.csv, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) indicate("Product FE=*.cpc_time") replace


*Table 9--Correlation between Foreign and Domestic Demand
gen l_testa = ln_a2b
gen l_testa_dom_temp = l_testa
replace l_testa_dom_temp = . if dest ~="dom"
bys CIIU id cpc3 time: egen l_testa_dom = mean(l_testa_dom_temp)

areg l_testa l_testa_dom if dest ~= "dom" & dexp_ftp == 1, absorb(cpc_market_time)
estimates store l_d_for_dom_corr
estout l_d_for_dom_corr, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0))
esttab l_d_for_dom_corr using table9.csv, cells(b(star fmt(3)) se(par fmt(2))) stats(r2 N, fmt(2 0)) replace
   


***FIGURES***

*Figure 1--Kernel Density of Productivity, Marginal Cost, and Domestic Markups, by Export Status
xi: reg logprod  i.cpc3*i.time if dest == "dom"
predict logprod_res if e(sample), resid
label var logprod_res "Log Productivity"
twoway kdensity logprod_res if dexp_ftp == 0 & logprod_res < 4 & logprod_res > -4 || kdensity logprod_res if dexp_ftp == 1 & logprod_res <4 & logprod_res > -4, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Productivity) saving(Figure1a.gph, replace)

xi: reg l_mc  i.cpc3*i.time if dest == "dom"
predict l_mc_res if e(sample), resid
label var l_mc_res "Log Marginal Cost"
twoway kdensity l_mc_res if dexp_ftp == 0 & l_mc_res > -2.5 & l_mc_res < 2.5 || kdensity l_mc_res if dexp_ftp == 1 & l_mc_res > -2.5 & l_mc_res < 2.5, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Marginal Cost) xscale(range(-2,2)) saving(Figure1b.gph, replace)

xi: reg l_markup  i.cpc3*i.time if dest == "dom"
predict l_markup_res if e(sample), resid
label var l_markup_res "Log Markup"
twoway kdensity l_markup_res if dexp_ftp == 0 & l_markup_res > -2 & l_markup_res < 3 || kdensity l_markup_res if dexp_ftp == 1 & l_markup_res > -2 & l_markup_res < 3, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Markup) saving(Figure1c.gph, replace)


*Figure 2--Kernel Density of Domestic Demand Location Parameter, Domestic Demand Slope, and Marginal Cost Curve Slope Parameter, by Export Status
xi: reg l_a  i.cpc3*i.time if dest == "dom"
predict l_a_res if e(sample), resid
label var l_a_res "Log Domestic Demand Location"
twoway kdensity l_a_res if dexp_ftp == 0 & l_a_res > -6 || kdensity l_a_res if dexp_ftp == 1 & l_a_res > -6, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Demand Location) saving(Figure2a.gph, replace)

xi: reg l_b  i.cpc3*i.time if dest == "dom"
predict l_b_res if e(sample), resid
label var l_b_res "Log Domestic Demand Slope"
twoway kdensity l_b_res if dexp_ftp == 0 & l_b_res > - 15 || kdensity l_b_res if dexp_ftp == 1 & l_b_res > - 15, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Demand Slope) saving(Figure2b.gph, replace)

gen l_cost_het = ai*ln(int_input_price) - logprod
xi: reg l_cost_het  i.cpc3*i.time if dest == "dom"
predict l_cost_het_res if e(sample), resid
label var l_cost_het_res "Log Input-Price Weighted Productivity"
twoway kdensity l_cost_het_res if dexp_ftp == 0 & l_cost_het_res > -5 & l_cost_het_res < 5 || kdensity l_cost_het_res if dexp_ftp == 1 & l_cost_het_res > -5 & l_cost_het_res < 5, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Input-Price Weighted Productivity) saving(Figure2c.gph, replace)


*Figure 3--Domestic Profitability Index, by Export Status
xi: reg l_profit  i.cpc3*i.time if dest == "dom"
predict l_profit_res if e(sample), resid
label var l_profit_res "Log Domestic Profitability Index"
twoway kdensity l_profit_res if dexp_ftp == 0 || kdensity l_profit_res if dexp_ftp == 1, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Profit Index) saving(Figure3a.gph, replace)

xi: reg l_profit_alt  i.cpc3*i.time if dest == "dom"
predict l_profit_alt_res if e(sample), resid
label var l_profit_alt_res "Log Domestic Profitability Index"
twoway kdensity l_profit_alt_res if dexp_ftp == 0 || kdensity l_profit_alt_res if dexp_ftp == 1, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Profit Index) saving(Figure3b.gph, replace)

gen l_ac = ln(a-mc_ftp)
gen l_profit_lr=2*l_ac-ln(4)-l_b 
xi: reg l_profit_lr  i.cpc3*i.time if dest == "dom"
predict l_profit_lr_res if e(sample), resid
label var l_profit_lr_res "Log Domestic Profitability Index"
twoway kdensity l_profit_lr_res if dexp_ftp == 0 & l_profit_lr_res > -10 || kdensity l_profit_lr_res if dexp_ftp == 1 & l_profit_lr_res > -10, lpattern(dash) legend(label(1 "Non-Exporters") label(2 "Exporters")) yti("") xti(Log Profit Index) saving(Figure3c.gph, replace)


*Figure 4--Scatter Plot of a^2/b and c/b, by Export Status
xi: reg ln_a2b i.cpc3*i.time if dest == "dom"
predict ln_a2b_res, resid
label var ln_a2b_res "Log Demand-Side Heterogeneity"

xi: reg ln_c0b i.cpc3*i.time if dest == "dom"
predict ln_c0b_res, resid
label var ln_c0b_res "Log Cost-Side Heterogeneity"

twoway (scatter ln_c0b_res ln_a2b_res if dest =="dom" & dexp_ftp == 0, msymbol(Oh) mcolor("gs10")) (scatter ln_c0b_res ln_a2b_res if dest =="dom" & dexp_ftp == 1, msymbol(x) mcolor("gs2")), legend(label(1 "Non-Exporters")) legend(label(2 "Exporters")) saving(Figure4.gph, replace)


*Figure 5--Domestic and Foreign Demand Relationship
areg l_testa if dest ~= "dom" & dexp_ftp == 1, absorb(cpc_market_time) 
predict l_testa_for_res if e(sample), resid

areg l_testa_dom if dest == "dom" & dexp_ftp == 1, absorb(cpc_market_time) 
predict l_testa_dom_res_temp if e(sample), resid
bys id cpc3 time: egen l_testa_dom_res = mean(l_testa_dom_res_temp)

bys id time cpc3: egen ave_l_testa_for_res = mean(l_testa_for_res)

twoway scatter ave_l_testa_for_res l_testa_dom_res if ave_l_testa_for_res ~= 0 & l_testa_dom_res ~= 0, title("Foreign versus Domestic Demand Relation") sub("Average Across Foreign Destinations") ytitle(Foreign Demand) xtitle(Domestic Demand) legend(off) xscale(range(-7,7)) xtick(-5(5)10) xlabel(-5(5)10) saving(Figure5.gph, replace)


